{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = './data/train_subset_1000000.csv'\n",
    "df = pd.read_csv(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--All data:999999\n",
      "--1 data:160219\n",
      "--0 data:839780\n",
      "--0 VS 1 => 5.24:1\n"
     ]
    }
   ],
   "source": [
    "print(f'--All data:{df.id.count()}')\n",
    "y_1_nums = df[df[\"click\"] == 1].id.count()\n",
    "y_0_nums = df[df[\"click\"] == 0].id.count()\n",
    "print(f'--1 data:{y_1_nums}')\n",
    "print(f'--0 data:{y_0_nums}')\n",
    "print(f'--0 VS 1 => {round(y_0_nums/y_1_nums,2)}:1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 999999 entries, 0 to 999998\n",
      "Data columns (total 24 columns):\n",
      "id                  999999 non-null float64\n",
      "click               999999 non-null int64\n",
      "hour                999999 non-null int64\n",
      "C1                  999999 non-null int64\n",
      "banner_pos          999999 non-null int64\n",
      "site_id             999999 non-null object\n",
      "site_domain         999999 non-null object\n",
      "site_category       999999 non-null object\n",
      "app_id              999999 non-null object\n",
      "app_domain          999999 non-null object\n",
      "app_category        999999 non-null object\n",
      "device_id           999999 non-null object\n",
      "device_ip           999999 non-null object\n",
      "device_model        999999 non-null object\n",
      "device_type         999999 non-null int64\n",
      "device_conn_type    999999 non-null int64\n",
      "C14                 999999 non-null int64\n",
      "C15                 999999 non-null int64\n",
      "C16                 999999 non-null int64\n",
      "C17                 999999 non-null int64\n",
      "C18                 999999 non-null int64\n",
      "C19                 999999 non-null int64\n",
      "C20                 999999 non-null int64\n",
      "C21                 999999 non-null int64\n",
      "dtypes: float64(1), int64(14), object(9)\n",
      "memory usage: 183.1+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 999999 entries, 0 to 999998\n",
      "Data columns (total 24 columns):\n",
      "id                  999999 non-null float64\n",
      "click               999999 non-null int64\n",
      "hour                999999 non-null int64\n",
      "C1                  999999 non-null int64\n",
      "banner_pos          999999 non-null int64\n",
      "site_id             999999 non-null object\n",
      "site_domain         999999 non-null object\n",
      "site_category       999999 non-null object\n",
      "app_id              999999 non-null object\n",
      "app_domain          999999 non-null object\n",
      "app_category        999999 non-null object\n",
      "device_id           999999 non-null object\n",
      "device_ip           999999 non-null object\n",
      "device_model        999999 non-null object\n",
      "device_type         999999 non-null int64\n",
      "device_conn_type    999999 non-null int64\n",
      "C14                 999999 non-null int64\n",
      "C15                 999999 non-null int64\n",
      "C16                 999999 non-null int64\n",
      "C17                 999999 non-null int64\n",
      "C18                 999999 non-null int64\n",
      "C19                 999999 non-null int64\n",
      "C20                 999999 non-null int64\n",
      "C21                 999999 non-null int64\n",
      "dtypes: float64(1), int64(14), object(9)\n",
      "memory usage: 183.1+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>click</th>\n",
       "      <th>app_id</th>\n",
       "      <th>app_domain</th>\n",
       "      <th>app_category</th>\n",
       "      <th>device_id</th>\n",
       "      <th>device_ip</th>\n",
       "      <th>device_model</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000009e+18</td>\n",
       "      <td>0</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>a99f214a</td>\n",
       "      <td>ddd2926e</td>\n",
       "      <td>44956a24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.000017e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>a99f214a</td>\n",
       "      <td>96809ac8</td>\n",
       "      <td>711ee120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.000037e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>a99f214a</td>\n",
       "      <td>b3cf8def</td>\n",
       "      <td>8a4875bd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.000064e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>a99f214a</td>\n",
       "      <td>e8275b8f</td>\n",
       "      <td>6332421a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.000068e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>ecad2386</td>\n",
       "      <td>7801e8d9</td>\n",
       "      <td>07d7df22</td>\n",
       "      <td>a99f214a</td>\n",
       "      <td>9644d0bf</td>\n",
       "      <td>779d90c2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             id  click    app_id app_domain app_category device_id device_ip  \\\n",
       "0  1.000009e+18      0  ecad2386   7801e8d9     07d7df22  a99f214a  ddd2926e   \n",
       "1  1.000017e+19      0  ecad2386   7801e8d9     07d7df22  a99f214a  96809ac8   \n",
       "2  1.000037e+19      0  ecad2386   7801e8d9     07d7df22  a99f214a  b3cf8def   \n",
       "3  1.000064e+19      0  ecad2386   7801e8d9     07d7df22  a99f214a  e8275b8f   \n",
       "4  1.000068e+19      0  ecad2386   7801e8d9     07d7df22  a99f214a  9644d0bf   \n",
       "\n",
       "  device_model  \n",
       "0     44956a24  \n",
       "1     711ee120  \n",
       "2     8a4875bd  \n",
       "3     6332421a  \n",
       "4     779d90c2  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[[\"id\",\"click\",\"app_id\",\"app_domain\",\"app_category\",\"device_id\",\"device_ip\",\"device_model\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>9.376309e+18</td>\n",
       "      <td>5.236908e+18</td>\n",
       "      <td>9.984920e+12</td>\n",
       "      <td>4.846660e+18</td>\n",
       "      <td>9.834382e+18</td>\n",
       "      <td>1.373053e+19</td>\n",
       "      <td>1.844670e+19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>click</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.602192e-01</td>\n",
       "      <td>3.668094e-01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>hour</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.410210e+07</td>\n",
       "      <td>1.493255e+00</td>\n",
       "      <td>1.410210e+07</td>\n",
       "      <td>1.410210e+07</td>\n",
       "      <td>1.410210e+07</td>\n",
       "      <td>1.410210e+07</td>\n",
       "      <td>1.410210e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C1</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.005088e+03</td>\n",
       "      <td>1.156928e+00</td>\n",
       "      <td>1.001000e+03</td>\n",
       "      <td>1.005000e+03</td>\n",
       "      <td>1.005000e+03</td>\n",
       "      <td>1.005000e+03</td>\n",
       "      <td>1.012000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>banner_pos</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>2.299222e-01</td>\n",
       "      <td>4.646270e-01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>7.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_type</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.025540e+00</td>\n",
       "      <td>4.538988e-01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>5.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_conn_type</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>2.233602e-01</td>\n",
       "      <td>6.671590e-01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>5.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C14</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.826220e+04</td>\n",
       "      <td>3.510366e+03</td>\n",
       "      <td>3.750000e+02</td>\n",
       "      <td>1.570700e+04</td>\n",
       "      <td>1.925100e+04</td>\n",
       "      <td>2.115300e+04</td>\n",
       "      <td>2.170500e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C15</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>3.189658e+02</td>\n",
       "      <td>1.945291e+01</td>\n",
       "      <td>1.200000e+02</td>\n",
       "      <td>3.200000e+02</td>\n",
       "      <td>3.200000e+02</td>\n",
       "      <td>3.200000e+02</td>\n",
       "      <td>1.024000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C16</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>5.649555e+01</td>\n",
       "      <td>3.654696e+01</td>\n",
       "      <td>2.000000e+01</td>\n",
       "      <td>5.000000e+01</td>\n",
       "      <td>5.000000e+01</td>\n",
       "      <td>5.000000e+01</td>\n",
       "      <td>1.024000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C17</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>2.041031e+03</td>\n",
       "      <td>4.412010e+02</td>\n",
       "      <td>1.120000e+02</td>\n",
       "      <td>1.722000e+03</td>\n",
       "      <td>2.161000e+03</td>\n",
       "      <td>2.420000e+03</td>\n",
       "      <td>2.497000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C18</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.452260e+00</td>\n",
       "      <td>1.362637e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C19</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>1.907794e+02</td>\n",
       "      <td>2.734394e+02</td>\n",
       "      <td>3.300000e+01</td>\n",
       "      <td>3.500000e+01</td>\n",
       "      <td>3.900000e+01</td>\n",
       "      <td>2.970000e+02</td>\n",
       "      <td>1.835000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C20</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>4.550590e+04</td>\n",
       "      <td>4.984381e+04</td>\n",
       "      <td>-1.000000e+00</td>\n",
       "      <td>-1.000000e+00</td>\n",
       "      <td>-1.000000e+00</td>\n",
       "      <td>1.000840e+05</td>\n",
       "      <td>1.002480e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C21</th>\n",
       "      <td>999999.0</td>\n",
       "      <td>6.993616e+01</td>\n",
       "      <td>3.851384e+01</td>\n",
       "      <td>1.300000e+01</td>\n",
       "      <td>4.300000e+01</td>\n",
       "      <td>6.100000e+01</td>\n",
       "      <td>7.900000e+01</td>\n",
       "      <td>1.950000e+02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     count          mean           std           min  \\\n",
       "id                999999.0  9.376309e+18  5.236908e+18  9.984920e+12   \n",
       "click             999999.0  1.602192e-01  3.668094e-01  0.000000e+00   \n",
       "hour              999999.0  1.410210e+07  1.493255e+00  1.410210e+07   \n",
       "C1                999999.0  1.005088e+03  1.156928e+00  1.001000e+03   \n",
       "banner_pos        999999.0  2.299222e-01  4.646270e-01  0.000000e+00   \n",
       "device_type       999999.0  1.025540e+00  4.538988e-01  0.000000e+00   \n",
       "device_conn_type  999999.0  2.233602e-01  6.671590e-01  0.000000e+00   \n",
       "C14               999999.0  1.826220e+04  3.510366e+03  3.750000e+02   \n",
       "C15               999999.0  3.189658e+02  1.945291e+01  1.200000e+02   \n",
       "C16               999999.0  5.649555e+01  3.654696e+01  2.000000e+01   \n",
       "C17               999999.0  2.041031e+03  4.412010e+02  1.120000e+02   \n",
       "C18               999999.0  1.452260e+00  1.362637e+00  0.000000e+00   \n",
       "C19               999999.0  1.907794e+02  2.734394e+02  3.300000e+01   \n",
       "C20               999999.0  4.550590e+04  4.984381e+04 -1.000000e+00   \n",
       "C21               999999.0  6.993616e+01  3.851384e+01  1.300000e+01   \n",
       "\n",
       "                           25%           50%           75%           max  \n",
       "id                4.846660e+18  9.834382e+18  1.373053e+19  1.844670e+19  \n",
       "click             0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  \n",
       "hour              1.410210e+07  1.410210e+07  1.410210e+07  1.410210e+07  \n",
       "C1                1.005000e+03  1.005000e+03  1.005000e+03  1.012000e+03  \n",
       "banner_pos        0.000000e+00  0.000000e+00  0.000000e+00  7.000000e+00  \n",
       "device_type       1.000000e+00  1.000000e+00  1.000000e+00  5.000000e+00  \n",
       "device_conn_type  0.000000e+00  0.000000e+00  0.000000e+00  5.000000e+00  \n",
       "C14               1.570700e+04  1.925100e+04  2.115300e+04  2.170500e+04  \n",
       "C15               3.200000e+02  3.200000e+02  3.200000e+02  1.024000e+03  \n",
       "C16               5.000000e+01  5.000000e+01  5.000000e+01  1.024000e+03  \n",
       "C17               1.722000e+03  2.161000e+03  2.420000e+03  2.497000e+03  \n",
       "C18               0.000000e+00  1.000000e+00  3.000000e+00  3.000000e+00  \n",
       "C19               3.500000e+01  3.900000e+01  2.970000e+02  1.835000e+03  \n",
       "C20              -1.000000e+00 -1.000000e+00  1.000840e+05  1.002480e+05  \n",
       "C21               4.300000e+01  6.100000e+01  7.900000e+01  1.950000e+02  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "##接下来对特征进行处理，先将类别特征进行编码\n",
    "#针对类型类的特征，先进行编码，编码之前构建字典\n",
    "from sklearn import preprocessing\n",
    "\n",
    "def label_encode(field,df):\n",
    "    dic = []\n",
    "    df_field = df[field]\n",
    "    list_field = df_field.tolist()\n",
    "\n",
    "    #构建field字典\n",
    "    for i in list_field:\n",
    "        if i not in dic:\n",
    "            dic.append(i)\n",
    "\n",
    "    label_field = preprocessing.LabelEncoder()\n",
    "    label_field.fit(dic)\n",
    "\n",
    "    df_field_enconde_tmp = label_field.transform(df_field)\n",
    "    df_field_enconde = pd.DataFrame(df_field_enconde_tmp, index=df.index, columns=[(field+'_enconde')])\n",
    "    return df_field_enconde"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 62400 entries, 8 to 63476\n",
      "Data columns (total 24 columns):\n",
      "id                  62400 non-null float64\n",
      "click               62400 non-null int64\n",
      "hour                62400 non-null int64\n",
      "C1                  62400 non-null int64\n",
      "banner_pos          62400 non-null int64\n",
      "site_id             62400 non-null object\n",
      "site_domain         62400 non-null object\n",
      "site_category       62400 non-null object\n",
      "app_id              62400 non-null object\n",
      "app_domain          62400 non-null object\n",
      "app_category        62400 non-null object\n",
      "device_id           62400 non-null object\n",
      "device_ip           62400 non-null object\n",
      "device_model        62400 non-null object\n",
      "device_type         62400 non-null int64\n",
      "device_conn_type    62400 non-null int64\n",
      "C14                 62400 non-null int64\n",
      "C15                 62400 non-null int64\n",
      "C16                 62400 non-null int64\n",
      "C17                 62400 non-null int64\n",
      "C18                 62400 non-null int64\n",
      "C19                 62400 non-null int64\n",
      "C20                 62400 non-null int64\n",
      "C21                 62400 non-null int64\n",
      "dtypes: float64(1), int64(14), object(9)\n",
      "memory usage: 11.9+ MB\n"
     ]
    }
   ],
   "source": [
    "##100万数据实在跑不过来，为了演示，再做截断\n",
    "df_click_1 =  df[df[\"click\"] == 1].iloc[:10000,:]\n",
    "df_click_0 =  df[df[\"click\"] == 0].iloc[:52400,:]\n",
    "\n",
    "#然后合并回去（行合并），作为新的df\n",
    "df=pd.concat([df_click_1, df_click_0])\n",
    "\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# site_id             999999 non-null object\n",
    "# site_domain         999999 non-null object\n",
    "# site_category       999999 non-null object\n",
    "# app_id              999999 non-null object\n",
    "# app_domain          999999 non-null object\n",
    "# app_category        999999 non-null object\n",
    "# device_id           999999 non-null object\n",
    "# device_ip           999999 non-null object\n",
    "# device_model        999999 non-null object\n",
    "df_site_id_enconde = label_encode('site_id',df)\n",
    "df_site_domain_enconde = label_encode('site_domain',df)\n",
    "df_site_category_enconde = label_encode('site_category',df)\n",
    "df_app_id_enconde = label_encode('app_id',df)\n",
    "df_app_domain_enconde = label_encode('app_domain',df)\n",
    "df_app_category_enconde = label_encode('app_category',df)\n",
    "df_device_id_enconde = label_encode('device_id',df)\n",
    "df_device_ip_enconde = label_encode('device_ip',df)\n",
    "df_device_model_enconde = label_encode('device_model',df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "#拼接特征回去\n",
    "# id                  999999 non-null float64\n",
    "# click               999999 non-null int64\n",
    "# hour                999999 non-null int64\n",
    "# C1                  999999 non-null int64\n",
    "# banner_pos          999999 non-null int64\n",
    "# site_id             999999 non-null object\n",
    "# site_domain         999999 non-null object\n",
    "# site_category       999999 non-null object\n",
    "# app_id              999999 non-null object\n",
    "# app_domain          999999 non-null object\n",
    "# app_category        999999 non-null object\n",
    "# device_id           999999 non-null object\n",
    "# device_ip           999999 non-null object\n",
    "# device_model        999999 non-null object\n",
    "# device_type         999999 non-null int64\n",
    "# device_conn_type    999999 non-null int64\n",
    "# C14                 999999 non-null int64\n",
    "# C15                 999999 non-null int64\n",
    "# C16                 999999 non-null int64\n",
    "# C17                 999999 non-null int64\n",
    "# C18                 999999 non-null int64,\n",
    "# C19                 999999 non-null int64\n",
    "# C20                 999999 non-null int64\n",
    "# C21                 999999 non-null int64\n",
    "df_input = pd.concat([df[['click','banner_pos','device_type','device_conn_type'\n",
    "                          ,'C1','C14','C15','C16','C17','C18','C19','C20','C21']]\n",
    "                      ,df_site_id_enconde\n",
    "                      ,df_site_domain_enconde\n",
    "                      ,df_site_category_enconde\n",
    "                      ,df_app_id_enconde\n",
    "                      ,df_app_domain_enconde\n",
    "                      ,df_app_category_enconde\n",
    "                      ,df_device_id_enconde\n",
    "                      ,df_device_ip_enconde\n",
    "                      ,df_device_model_enconde], axis=1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>click</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>0.160256</td>\n",
       "      <td>0.366847</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>banner_pos</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>0.198013</td>\n",
       "      <td>0.402148</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_type</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>1.059631</td>\n",
       "      <td>0.591574</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_conn_type</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>0.199183</td>\n",
       "      <td>0.634956</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C1</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>1005.041779</td>\n",
       "      <td>1.099718</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>1005.0</td>\n",
       "      <td>1005.0</td>\n",
       "      <td>1005.00</td>\n",
       "      <td>1010.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C14</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>17701.539696</td>\n",
       "      <td>3228.240792</td>\n",
       "      <td>375.0</td>\n",
       "      <td>15704.0</td>\n",
       "      <td>17654.0</td>\n",
       "      <td>20362.00</td>\n",
       "      <td>21705.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C15</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>318.395256</td>\n",
       "      <td>11.819491</td>\n",
       "      <td>120.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>320.00</td>\n",
       "      <td>728.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C16</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>56.676410</td>\n",
       "      <td>36.533532</td>\n",
       "      <td>20.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>50.00</td>\n",
       "      <td>480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C17</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>1966.523093</td>\n",
       "      <td>393.891028</td>\n",
       "      <td>112.0</td>\n",
       "      <td>1722.0</td>\n",
       "      <td>1993.0</td>\n",
       "      <td>2307.00</td>\n",
       "      <td>2497.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C18</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>0.789439</td>\n",
       "      <td>1.226222</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C19</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>130.603077</td>\n",
       "      <td>241.374198</td>\n",
       "      <td>33.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>39.00</td>\n",
       "      <td>1835.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C20</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>38018.727035</td>\n",
       "      <td>48582.414092</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>100083.00</td>\n",
       "      <td>100248.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C21</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>88.775721</td>\n",
       "      <td>45.489415</td>\n",
       "      <td>13.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>156.00</td>\n",
       "      <td>157.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>site_id_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>289.941779</td>\n",
       "      <td>204.245522</td>\n",
       "      <td>0.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>268.0</td>\n",
       "      <td>404.00</td>\n",
       "      <td>760.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>site_domain_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>467.118638</td>\n",
       "      <td>169.278146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>389.0</td>\n",
       "      <td>492.0</td>\n",
       "      <td>619.00</td>\n",
       "      <td>646.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>site_category_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>5.829038</td>\n",
       "      <td>4.456125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.00</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>app_id_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>523.173958</td>\n",
       "      <td>131.784304</td>\n",
       "      <td>0.0</td>\n",
       "      <td>572.0</td>\n",
       "      <td>572.0</td>\n",
       "      <td>572.00</td>\n",
       "      <td>610.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>app_domain_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>20.128926</td>\n",
       "      <td>5.550983</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>21.00</td>\n",
       "      <td>47.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>app_category_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>1.169087</td>\n",
       "      <td>3.480267</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_id_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>3291.346987</td>\n",
       "      <td>603.055366</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3405.0</td>\n",
       "      <td>3405.0</td>\n",
       "      <td>3405.00</td>\n",
       "      <td>5049.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_ip_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>14718.134888</td>\n",
       "      <td>8451.461740</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7638.0</td>\n",
       "      <td>14525.5</td>\n",
       "      <td>22008.25</td>\n",
       "      <td>29532.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>device_model_enconde</th>\n",
       "      <td>62400.0</td>\n",
       "      <td>1099.835000</td>\n",
       "      <td>602.001177</td>\n",
       "      <td>0.0</td>\n",
       "      <td>560.0</td>\n",
       "      <td>1148.0</td>\n",
       "      <td>1634.00</td>\n",
       "      <td>2184.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         count          mean           std     min      25%  \\\n",
       "click                  62400.0      0.160256      0.366847     0.0      0.0   \n",
       "banner_pos             62400.0      0.198013      0.402148     0.0      0.0   \n",
       "device_type            62400.0      1.059631      0.591574     0.0      1.0   \n",
       "device_conn_type       62400.0      0.199183      0.634956     0.0      0.0   \n",
       "C1                     62400.0   1005.041779      1.099718  1001.0   1005.0   \n",
       "C14                    62400.0  17701.539696   3228.240792   375.0  15704.0   \n",
       "C15                    62400.0    318.395256     11.819491   120.0    320.0   \n",
       "C16                    62400.0     56.676410     36.533532    20.0     50.0   \n",
       "C17                    62400.0   1966.523093    393.891028   112.0   1722.0   \n",
       "C18                    62400.0      0.789439      1.226222     0.0      0.0   \n",
       "C19                    62400.0    130.603077    241.374198    33.0     35.0   \n",
       "C20                    62400.0  38018.727035  48582.414092    -1.0     -1.0   \n",
       "C21                    62400.0     88.775721     45.489415    13.0     61.0   \n",
       "site_id_enconde        62400.0    289.941779    204.245522     0.0     88.0   \n",
       "site_domain_enconde    62400.0    467.118638    169.278146     0.0    389.0   \n",
       "site_category_enconde  62400.0      5.829038      4.456125     0.0      2.0   \n",
       "app_id_enconde         62400.0    523.173958    131.784304     0.0    572.0   \n",
       "app_domain_enconde     62400.0     20.128926      5.550983     0.0     21.0   \n",
       "app_category_enconde   62400.0      1.169087      3.480267     0.0      0.0   \n",
       "device_id_enconde      62400.0   3291.346987    603.055366     0.0   3405.0   \n",
       "device_ip_enconde      62400.0  14718.134888   8451.461740     0.0   7638.0   \n",
       "device_model_enconde   62400.0   1099.835000    602.001177     0.0    560.0   \n",
       "\n",
       "                           50%        75%       max  \n",
       "click                      0.0       0.00       1.0  \n",
       "banner_pos                 0.0       0.00       5.0  \n",
       "device_type                1.0       1.00       5.0  \n",
       "device_conn_type           0.0       0.00       5.0  \n",
       "C1                      1005.0    1005.00    1010.0  \n",
       "C14                    17654.0   20362.00   21705.0  \n",
       "C15                      320.0     320.00     728.0  \n",
       "C16                       50.0      50.00     480.0  \n",
       "C17                     1993.0    2307.00    2497.0  \n",
       "C18                        0.0       2.00       3.0  \n",
       "C19                       35.0      39.00    1835.0  \n",
       "C20                       -1.0  100083.00  100248.0  \n",
       "C21                       79.0     156.00     157.0  \n",
       "site_id_enconde          268.0     404.00     760.0  \n",
       "site_domain_enconde      492.0     619.00     646.0  \n",
       "site_category_enconde      4.0       6.00      15.0  \n",
       "app_id_enconde           572.0     572.00     610.0  \n",
       "app_domain_enconde        21.0      21.00      47.0  \n",
       "app_category_enconde       0.0       0.00      18.0  \n",
       "device_id_enconde       3405.0    3405.00    5049.0  \n",
       "device_ip_enconde      14525.5   22008.25   29532.0  \n",
       "device_model_enconde    1148.0    1634.00    2184.0  "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_input.describe().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "#对数据进行分割，分割为训练集和测试集\n",
    "x_train,x_test,y_train,y_test = train_test_split(df_input.iloc[:,1:],df_input[\"click\"],test_size=0.3, random_state=123) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "##导入XGB相关的库\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn import metrics\n",
    "from sklearn.externals import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Time : 2020-04-18 10:27:44\n",
      "End Time : 2020-04-18 10:27:45\n"
     ]
    }
   ],
   "source": [
    "##进行xgboost拟合\n",
    "import time \n",
    "\n",
    "begin_time = time.time()\n",
    "print(f'Begin Time : {time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(begin_time))}')\n",
    "\n",
    "##受限于机器的资源，这里就不做gridsearch调参了，直接凑合着来(按最小资源消耗来设置参数)\n",
    "model = XGBClassifier(learning_rate=0.1\n",
    "                     ,n_estimators=10\n",
    "                     ,max_depth=3\n",
    "                     ,objective='binary:logistic'\n",
    "                     )\n",
    "\n",
    "model.fit(x_train, y_train, eval_metric=\"auc\")\n",
    "\n",
    "end_time = time.time()\n",
    "print(f'End Time : {time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(end_time))}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./model/xgb_model.pkl']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##保存xgb的model\n",
    "joblib.dump(model, './model/xgb_model.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "##效果输出函数\n",
    "def func_print_score(x_data,y_data,data_type,model_x):\n",
    "    y_pred = model_x.predict(x_data)\n",
    "    \n",
    "    print(f'==============({data_type})===================')\n",
    "    confusion = metrics.confusion_matrix(y_data, y_pred)\n",
    "    print(confusion)\n",
    "    \n",
    "    print('------------------------')\n",
    "    auc = metrics.roc_auc_score(y_data,y_pred)\n",
    "    print(f'AUC: {auc}')\n",
    "    \n",
    "    print('------------------------')\n",
    "    accuracy = metrics.accuracy_score(y_data,y_pred)\n",
    "    print(f'Accuracy: {accuracy}')\n",
    "    \n",
    "    print('------------------------')\n",
    "    report = metrics.classification_report(y_data, y_pred)\n",
    "    print(report) \n",
    "    \n",
    "    print('=============================================')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==============(testdata-xgb)===================\n",
      "[[15576   196]\n",
      " [ 2765   183]]\n",
      "------------------------\n",
      "AUC: 0.5248244488713144\n",
      "------------------------\n",
      "Accuracy: 0.8418269230769231\n",
      "------------------------\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.85      0.99      0.91     15772\n",
      "          1       0.48      0.06      0.11      2948\n",
      "\n",
      "avg / total       0.79      0.84      0.79     18720\n",
      "\n",
      "=============================================\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/data/python/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n"
     ]
    }
   ],
   "source": [
    "func_print_score(x_test,y_test,'testdata-xgb', model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
